﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CMTVision.WidgetWeb.Widgets
{
    public partial class ManagePageWidget : CMTVision.Widgets.Framework.BaseWidget
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #region Bind Data

        protected void BindData()
        {
            using (Model.CMTWidgetsEntities entities = Common.EntitiesManager.LoadWidgetEntity())
            {
                var allTemplateContainers = (from tc in entities.TemplateContainers.Include("Template.TemplateType")
                                             where tc.Template!=null 
                                                && tc.Template.TemplateType!=null
                                             select new {
                                                 ID=tc.ID,
                                                 TemplateContainerName = tc.Name,
                                                 TemplateTypeName = tc.Template.TemplateType.Name
                                             }).ToList();
                listDataGrid.DataSource = allTemplateContainers;
                listDataGrid.DataBind();
            }

            using (Model.CMTWidgetsEntities entities = Common.EntitiesManager.LoadWidgetEntity())
            {
                var allTemplatesType = (from wt in entities.TemplateTypes select wt).ToList();
                TemplateTypeDropDownList.DataSource = allTemplatesType;
                TemplateTypeDropDownList.DataTextField = "Name";
                TemplateTypeDropDownList.DataValueField = "ID";
                TemplateTypeDropDownList.DataBind();
            }

            TemplateThinDropDownList.DataSource = new string[] { "Dark Hive", "UI lightness" };
            TemplateThinDropDownList.DataBind();

            TemplateColorDropDownList.DataSource = new string[] { "Blue", "Red", "Yellow" };
            TemplateColorDropDownList.DataBind();
        }

        #endregion

        #region Events

        protected void listDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            { 
                LinkButton editLinkButton = e.Item.FindControl("EditLinkButton") as LinkButton; 
                editLinkButton.PostBackUrl = "~/Default.aspx?id="+e.Item.Cells[0].Text;
            }
        }

        protected void addNewButton_Click(object sender, EventArgs e)
        {
            ShowPanel.Visible = false;
            EditPanel.Visible = true;
        }

        #endregion


        #region Drop Down List change events

        protected void TemplateTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void TemplateThinDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void TemplateColorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        #endregion

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            ShowPanel.Visible = true;
            EditPanel.Visible = false;
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            using (Model.CMTWidgetsEntities entities = Common.EntitiesManager.LoadWidgetEntity())
            {
                var selectedTemplateTypeID = Convert.ToInt32(TemplateTypeDropDownList.SelectedValue);
                var templateType = (from tt in entities.TemplateTypes where tt.ID == selectedTemplateTypeID select tt).FirstOrDefault();
                if (templateType != null)
                {
                    var template = new Model.Template();
                    template.TemplateType = templateType;
                    entities.AddToTemplates(template);

                    var templateContainer = new Model.TemplateContainer();
                    templateContainer.Template = template;
                    templateContainer.Name = NameTextBox.Text;
                    entities.AddToTemplateContainers(templateContainer);
                    entities.SaveChanges();
                }
            }
            BindData();
            ShowPanel.Visible = true;
            EditPanel.Visible = false;
        }
    }
}